﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sale_Operation;
using Sale_Common;
using Sale_Model;
using SaleSystem;
using ExtAspNet;


namespace SaleSystem.Other
{
    public partial class HomeList : PageBase
    {
        #region PageEvent
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetAndBind();
                GetDayInfo();
                GetMonthInfo();
            }
            else if (Request[LinkService.RefreshTag] == LinkService.RefreshString)
            {
                GetWorkPlan();

            }
         
        }

        protected void gv_List_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
           if (e.CommandName.ToLower() == "edit")
            {
                win_Content.Hidden = false;
                win_Content.IFrameUrl = "frm_WorkPlanEdit.aspx?Id=" + ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0]);
                win_Content.Title = "编辑-" + ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);
            }
        }
        #endregion
        #region Method
        private void GetAndBind()
        {
            GetWorkPlan();
        }
        private void GetWorkPlan()
        {
            WorkPlanOp wp = new WorkPlanOp();
            //获取前后5天内未完成的工作计划
            string str1 = System.DateTime.Now.AddDays(-5).ToString("yyyy-MM-dd");
            string str2 = System.DateTime.Now.AddDays(5).ToString("yyyy-MM-dd");
            this.gv_List.DataSource = wp.GetList(10, "(WPlan_StartTime >='" + str1 + "' AND WPlan_EndTime<='" + str2 + "' AND WPlan_State=0 ) OR (WPlan_StartTime >='" + str2 + "' AND WPlan_EndTime<='" + str2 + "' AND WPlan_State=0 )");
            this.gv_List.DataBind();

        }
        private void GetDayInfo()
        {
            AnalysisOp Manager = new AnalysisOp();
            string strStartTime = System.DateTime.Now.ToString("yyyy-MM-01");
            string strEndTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-") + DateTime.DaysInMonth(System.DateTime.Now.Year, System.DateTime.Now.Month).ToString()).ToString("yyyy-MM-dd");
            DataTable dt = Manager.GetList(3, System.DateTime.Now.ToString("yyyy-MM-01"), strEndTime);
      
            string strTip = "本月销售趋势图";
            //strTip = strStartTime + "至" + strEndTime;
            if (!dt.Columns.Contains("ShortDate"))
            {

                dt.Columns.Add("ShortDate");
                for (int i = 0; i < dt.Rows.Count; i++)
                {                   
                        dt.Rows[i]["ShortDate"] = DateTime.Parse(ValueHandler.GetStringDateValue(dt.Rows[i]["TimeSpin"])).ToString("dd");
               }

                }

            GetPie(WebChartControl3, dt, "ShortDate", "Sales",strTip );
            WebChartControl3.Height = Unit.Pixel(400);
            WebChartControl3.Width = Unit.Pixel(590);
 
        }
        private void GetMonthInfo()
        { 
            string strStartTime=System.DateTime.Now.ToString("yyyy-01-01");
            string strEndTime = System.DateTime.Now.ToString("yyyy-12-31");
            AnalysisOp Manager = new AnalysisOp();
            DataTable dt1 = Manager.GetList(2, strStartTime, strEndTime);
            if (!dt1.Columns.Contains("ShortDate"))
            {

                dt1.Columns.Add("ShortDate");
                for (int i = 0; i < dt1.Rows.Count; i++)
                {

                    dt1.Rows[i]["ShortDate"] = DateTime.Parse(ValueHandler.GetStringDateValue(dt1.Rows[i]["TimeSpin"])).ToString("yyyy-MM");
                
                    
                }
            } 
            GetPie(WebChartControl1, dt1, "ShortDate", "Sales", "月度销售比例");
            WebChartControl1.Height = Unit.Pixel(400);
            WebChartControl1.Width = Unit.Pixel(400);
            
        }
        #endregion
    }
}
